package Controller;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import Entity.Location;
import Model.LocationManageModel;
import View.LocationManageView;

public class LocationManageController {
	private boolean update = false;
	private LocationManageView view = null;
	private LocationManageModel model = null;
	private JFrame frame;

	public LocationManageController() {
		super();
		frame = new JFrame();
		frame.setSize(800, 600);
		view = new LocationManageView();
		try {
			model = new LocationManageModel();
			frame.add(view);
			view.setModel(model);
			doAction();		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		view.setEnableControl(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		frame.setBounds(screensize.width/2-400,screensize.height/2 - 300,800,600);
		frame.setVisible(true);
	}

	private void doAction() {
		// TODO Auto-generated method stub
		doAdd();
		doWrite();
		doEdit();
		doDelete();
		doExit();
		doSelectTable();
	}

	private void doSelectTable() {
		// TODO Auto-generated method stub
		view.doSelectTable(new KeyAdapter() {

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				doSelect();
			}

		});

		view.doSelectTable(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				doSelect();
			}

		});
	}

	private void doSelect() {
		int row = view.getSelectedItem();
		Location location = model.getLocationList().get(row);
		view.display(location);
		view.setEnableControl(false);
	}
	
	private void doAdd() {
		// TODO Auto-generated method stub
		view.doAdd(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Location location = view.getData();
				int row = view.getSelectedItem();
				if (location.getName().trim().equals("")) {
					JOptionPane.showMessageDialog(frame,
							"Ban chua nhap ten cho Location");
					return;
				}
				if(update){
						try {
							model.editLocation(row, location);
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (ClassNotFoundException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
				}
				else {
					try {
						model.addLocation(location);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ClassNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				view.setEnableControl(false);
		}
	});	
				view.setEnableControl(true);
			}
	
	private void doWrite() {
		// TODO Auto-generated method stub
		view.doWrite(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				view.display(new Location("",0));
				view.setEnableControl(true);
			}

		});

	}
	
	private void doEdit() {
		// TODO Auto-generated method stub
		view.doEdit(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				update = true;
				view.setEnableControl(true);
			}

		});
	}

	private void doDelete() {
		// TODO Auto-generated method stub
		view.doDelete(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int row = view.getSelectedItem();
				Location location = view.getData();
				try {
					model.deleteLocation(row, location);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}

		});
	}

	private void doExit() {
		// TODO Auto-generated method stub
		view.doExit(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				frame.dispose();
			}

		});
	}
	
	public static void main(String[] arg) throws IOException, ClassNotFoundException, SQLException  {
		new LocationManageController();
	}
	
}




